package com.hermit.qmst.service;

import android.app.IntentService;
import android.content.Intent;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.FileUtils;
import com.hermit.qmst.tools.ImageDownLoader;
import com.hermit.qmst.tools.Log;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by hermithermit on 15/4/1.
 */
public class GetImageService extends IntentService {


    public static final String TAG = "GetImageService";
    public GetImageService(){
        super("GetImageService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {

           request_advert();

    }

    //请求广告信息
    private void request_advert(){
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.getCache().clear();

        String url = Common.submitQueryPicPath();


        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,url,null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.i(TAG, "拨号等待页面广告" + jsonObject.toString(), true);
                        try {
                            FileUtils fileUtils = new FileUtils();
                            if (jsonObject != null && jsonObject.getInt("status") == 1){

                                JSONArray dataArr = jsonObject.getJSONArray("data");
                                for (int i = 0; i < dataArr.length() ;i++){
                                    JSONObject item = dataArr.getJSONObject(i);
                                    String url = item.getString("img");
                                    ImageDownLoader imageDownLoader = new ImageDownLoader(GetImageService.this);
                                    imageDownLoader.downloadImage(Common.getDialImageDirPath(),url);
                                }
                                fileUtils.saveTxtFile(Common.getDialImageDirPath(),Common.mCallWaitTxtFileName,dataArr.toString());

                            }else {
                                fileUtils.deleteFile(Common.getDialImageDirPath(),Common.mCallWaitTxtFileName);

                            }

                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }
        ){
            @Override
            public byte[] getBody() {
                String requestParam =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><type>dial_waiting_pic</type><uid>"+Common.mInfoId+"</uid><phone>"+ Common.myPhone +"</phone></data>";
                byte[] b = null;
                try {
                    b = requestParam.getBytes("utf-8");
                }catch (Exception e){
                    b = requestParam.getBytes();
                }
                return b;
            }
        };
        requestQueue.add(request);


    }
}
